# -*- encoding:utf8 -*-
from __future__ import unicode_literals
import sys
import json
import time
import requests
from ..models import AccessToken
from django.utils import timezone
from datetime import timedelta

from django.conf import settings
appid = settings.APPID
secret = settings.SECRET
# appid='wx7080c32bd10defb0'
# secret='d4624c36b6795d1d99dcf0547af5443d'
sys.weixin={}

class WXFW(object):
    def __init__(self,appid,secret):
        self.appid=appid
        self.secret=secret
    
    @property
    def token(self):
        now = timezone.now()
        start = now - timedelta(hours=1,minutes=50)
        try:
            token = AccessToken.objects.get(appid=self.appid,update__gte=start)
            return token.token
        except AccessToken.DoesNotExist:
            token_str = self.get_access_token_from_server()
            AccessToken.objects.create(appid=self.appid,token=token_str)
            return token_str
    
    def now_token(self):
        pass
    
    def get_access_token_from_server(self):
        """从服务器取token
        """
        url='https://api.weixin.qq.com/cgi-bin/token?grant_type=client_credential&appid={appid}&secret={secret}'.format(appid=self.appid,secret=self.secret)
        rq = requests.get(url)
        dc=json.loads(rq.text)
        access_token = dc.get("access_token")
        return access_token
    
    def get_openid(self,code):
        """url返回：
        {
        "access_token":"ACCESS_TOKEN",
        "expires_in":7200,
        "refresh_token":"REFRESH_TOKEN",
        "openid":"OPENID",
        "scope":"SCOPE",
        "unionid": "o6_bmasdasdsad6_2sgVt7hMZOPfL"
        }
        """
        url='https://api.weixin.qq.com/sns/oauth2/access_token?appid={appid}&secret={secret}&code={code}&grant_type=authorization_code'.format(appid=self.appid,secret=self.secret,code=code)
        rq = requests.get(url)
        dc = json.loads(rq.text)
        #self.token.save() # 刷新一下时间
        return dc.get('openid')   
    
    def get_userinfo(self,openid):
        """返回字典格式为：
        {
        "openid":" OPENID",
        " nickname": NICKNAME,
        "sex":"1",
        "province":"PROVINCE"
        "city":"CITY",
        "country":"COUNTRY",
        "headimgurl":    "http://wx.qlogo.cn/mmopen/g3MonUZtNHkdmzicIlibx6iaFqAc56vxLSUfpb6n5WKSYVY0ChQKkiaJSgQ1dZuTOgvLLrhJbERQQ4eMsv84eavHiaiceqxibJxCfHe/46", 
        "privilege":[
        "PRIVILEGE1"
        "PRIVILEGE2"
        ],
        "unionid": "o6_bmasdasdsad6_2sgVt7hMZOPfL"
    }
    更具体的见微信帮助：http://mp.weixin.qq.com/wiki/17/c0f37d5704f0b64713d5d2c37b468d75.html  的第四步
        """
        url='https://api.weixin.qq.com/sns/userinfo?access_token={token}&openid={openid}&lang=zh_CN'.format(token=self.token,openid=openid)
        rq=requests.get(url)
        rq.encoding='utf8'
        dc=json.loads(rq.text)
        return dc   
    
    def get_userlist(self):
        """从服务器拉取用户列表
        """
        url= 'https://api.weixin.qq.com/cgi-bin/user/get?access_token={token}'.format(token=self.token)
        rq = requests.get(url)
        dc = json.loads(rq.text)
        if dc.get('errcode'):
            return []
        else:
            return dc.get('data').get('openid')   
    
    def get_user_with_openids(openids):
        """向服务器查询不完整信息用户
        [
           {
               "subscribe": 1, 
               "openid": "otvxTs4dckWG7imySrJd6jSi0CWE", 
               "nickname": "iWithery", 
               "sex": 1, 
               "language": "zh_CN", 
               "city": "Jieyang", 
               "province": "Guangdong", 
               "country": "China", 
               "headimgurl": "http://wx.qlogo.cn/mmopen/xbIQx1GRqdvyqkMMhEaGOX802l1CyqMJNgUzKP8MeAeHFicRDSnZH7FY4XB7p8XHXIf6uJA2SCunTPicGKezDC4saKISzRj3nz/0", 
               "subscribe_time": 1434093047, 
               "unionid": "oR5GjjgEhCMJFyzaVZdrxZ2zRRF4", 
               "remark": "", 
               "groupid": 0
           }, ]
        """
        #access_token= get_access_token()
        # access_token = u'VH0ga_JPqa_qYJZSlrAkQFp1QdtitECd4llntEwQoIxMt8VT7DFq0tErYCWoeGFzux2j6rk7SUiGkaQabc3zjVjYYFEtp1lYF-8fYOwOvJwLLGiAFAHHC'
        url= 'https://api.weixin.qq.com/cgi-bin/user/info/batchget?access_token={token}'.format(self.token)
        cnt = 0
        items = []
        todic = {"user_list":[]}
        for openid in openids:       #-----------考虑到微信一次最多接受100个用户查询，
            cnt+=1
            todic['user_list'].append({
                "openid":openid,
                'lang':'zh-CN'
            })
            if cnt>90:               #----------所以这里就设置为90个
                data = json.dumps(todic)
                rq = requests.post(url,data=data)
                rq.encoding='utf8'
                rtdict= json.loads(rq.text)
                items.extend( rtdict.get( "user_info_list") )
                # 清空计数，容器
                cnt=0
                todic = {"user_list":[]}
        if todic['user_list']:
            data = json.dumps(todic)
            rq = requests.post(url,data=data,stream=True)
            rq.encoding='utf8'
            rtdict= json.loads(rq.text)
            try:
                items.extend( rtdict.get( "user_info_list") )
            except TypeError:
                print('未获取到用户信息，查看是否[1]次数过多[2]用户未关注该微信号')
    
        return items    
    

#def get_access_token():
    #"""获取token，如果没过期(1个小时)，就从缓存里面取，否则从服务器取。
    #注意：
        #尽管可能在分布式服务器上，全局变量不同，但是这里不要求sys.weixin同步，所以这里应该是没有问题的。
    #"""
    #access_token= sys.weixin.get('access_token')
    #if not access_token:
        #access_token = fetch_access_token()
    #else:
        #token_time =sys.weixin['token_time']
        #now = int(time.time())
        #if now -token_time >3600:
            #access_token=fetch_access_token()
    #return access_token


#def get_userinfo_switch(code,userinfo,msg=[]):
    #"""当同一台手机切换账号时，就可能是cookies有userinfo信息，
    #但是openid却不是原来的人了。
    #----TOdo 每次切换用户，或者退出登录，都会清空cookie，所以这个函数无用了。
    #"""
    #access_token = get_access_token()
    #open_dict = get_openid(code, msg)
    #openid = open_dict.get('openid')
    #openid_local = userinfo.get('openid')
    #ACCESS_TOKEN=open_dict.get('access_token')
    #if openid == openid_local:
        #oldtime=int( userinfo.get('time') )
        #now = int(time.time())
        #if now-oldtime >3600*24*2:
            #dc = get_userinfo(ACCESS_TOKEN, openid, msg)
            #dc['time']=now
            #msg.append('openid 的时间超过2天，更新')
            #return dc
        #else:
            #msg.append('userinfo没过期，openid也没变，不需要更新')
            #return userinfo
    #else:
        #msg.append('openid不同，肯定是切换用户了')
        #dc = get_userinfo(ACCESS_TOKEN, openid, msg)
        #dc['time']=int(time.time())
        #return dc

#def get_user_from_server(code,msg=[]):
    #"""从code到openid再到userinfo
    #"""
    #msg.append('从code->openid->userinfo')
    #dc = get_openid(code)
    #msg.append(dc)
    #ACCESS_TOKEN=dc.get('access_token')
    #openid=dc.get('openid') 
    #userinfo = get_userinfo(ACCESS_TOKEN, openid)
    #msg.append(userinfo)
    #userinfo['time'] = int(time.time())
    #return userinfo

#def get_openid(code):
    #"""返回：
    #{
    #"access_token":"ACCESS_TOKEN",
    #"expires_in":7200,
    #"refresh_token":"REFRESH_TOKEN",
    #"openid":"OPENID",
    #"scope":"SCOPE",
    #"unionid": "o6_bmasdasdsad6_2sgVt7hMZOPfL"
#}
    #"""
    #url='https://api.weixin.qq.com/sns/oauth2/access_token?appid='+appid+'&secret='+secret+'&code='+code+'&grant_type=authorization_code'
    #rq = requests.get(url)
    #dc = json.loads(rq.text)
    #return dc.get('openid')

#def get_userinfo(ACCESS_TOKEN,openid):
    #"""返回字典格式为：
    #{
    #"openid":" OPENID",
    #" nickname": NICKNAME,
    #"sex":"1",
    #"province":"PROVINCE"
    #"city":"CITY",
    #"country":"COUNTRY",
    #"headimgurl":    "http://wx.qlogo.cn/mmopen/g3MonUZtNHkdmzicIlibx6iaFqAc56vxLSUfpb6n5WKSYVY0ChQKkiaJSgQ1dZuTOgvLLrhJbERQQ4eMsv84eavHiaiceqxibJxCfHe/46", 
    #"privilege":[
    #"PRIVILEGE1"
    #"PRIVILEGE2"
    #],
    #"unionid": "o6_bmasdasdsad6_2sgVt7hMZOPfL"
#}
#更具体的见微信帮助：http://mp.weixin.qq.com/wiki/17/c0f37d5704f0b64713d5d2c37b468d75.html  的第四步
    #"""
    #url='https://api.weixin.qq.com/sns/userinfo?access_token='+ACCESS_TOKEN+'&openid='+openid+'&lang=zh_CN'
    #rq=requests.get(url)
    #rq.encoding='utf8'
    #dc=json.loads(rq.text)
    #return dc

#def fetch_access_token():
    #"""从服务器取token
    #"""
    #rq = requests.get('https://api.weixin.qq.com/cgi-bin/token?grant_type=client_credential&appid='+appid+'&secret='+secret)
    #dc=json.loads(rq.text)
    #access_token = dc.get("access_token")
    #token_time=int(time.time())
    #sys.weixin['access_token']=access_token
    #sys.weixin['token_time']=token_time  
    #return access_token

#def get_user_with_openids(openids):
    #"""向服务器查询不完整信息用户
    #[
       #{
           #"subscribe": 1, 
           #"openid": "otvxTs4dckWG7imySrJd6jSi0CWE", 
           #"nickname": "iWithery", 
           #"sex": 1, 
           #"language": "zh_CN", 
           #"city": "Jieyang", 
           #"province": "Guangdong", 
           #"country": "China", 
           #"headimgurl": "http://wx.qlogo.cn/mmopen/xbIQx1GRqdvyqkMMhEaGOX802l1CyqMJNgUzKP8MeAeHFicRDSnZH7FY4XB7p8XHXIf6uJA2SCunTPicGKezDC4saKISzRj3nz/0", 
           #"subscribe_time": 1434093047, 
           #"unionid": "oR5GjjgEhCMJFyzaVZdrxZ2zRRF4", 
           #"remark": "", 
           #"groupid": 0
       #}, ]
    #"""
    #access_token= get_access_token()
    ## access_token = u'VH0ga_JPqa_qYJZSlrAkQFp1QdtitECd4llntEwQoIxMt8VT7DFq0tErYCWoeGFzux2j6rk7SUiGkaQabc3zjVjYYFEtp1lYF-8fYOwOvJwLLGiAFAHHC'
    #url= 'https://api.weixin.qq.com/cgi-bin/user/info/batchget?access_token='+access_token
    #cnt = 0
    #items = []
    #todic = {"user_list":[]}
    #for openid in openids:       #-----------考虑到微信一次最多接受100个用户查询，
        #cnt+=1
        #todic['user_list'].append({
            #"openid":openid,
            #'lang':'zh-CN'
        #})
        #if cnt>90:               #----------所以这里就设置为90个
            #data = json.dumps(todic)
            #rq = requests.post(url,data=data)
            #rq.encoding='utf8'
            #rtdict= json.loads(rq.text)
            #items.extend( rtdict.get( "user_info_list") )
            ## 清空计数，容器
            #cnt=0
            #todic = {"user_list":[]}
    #if todic['user_list']:
        #data = json.dumps(todic)
        #rq = requests.post(url,data=data,stream=True)
        #rq.encoding='utf8'
        #rtdict= json.loads(rq.text)
        #try:
            #items.extend( rtdict.get( "user_info_list") )
        #except TypeError:
            #print('未获取到用户信息，查看是否[1]次数过多[2]用户未关注该微信号')

    #return items


#def get_userlist():
    #"""从服务器拉取用户列表
    #"""
    #access_token = get_access_token()
    #url= 'https://api.weixin.qq.com/cgi-bin/user/get?access_token='+access_token
    #rq = requests.get(url)
    #dc = json.loads(rq.text)
    #if dc.get('errcode'):
        #return []
    #else:
        #return dc.get('data').get('openid')

class WXFW0(WXFW):
    def token(self):
        return self.get_access_token_from_server()

if __name__ =='__main__':
    wx=WXFW0(appid='wx7080c32bd10defb0',secret='d4624c36b6795d1d99dcf0547af5443d')
    dc = wx.get_userinfo('o6hHBv6heaheBEvoPoJMEcsKXyZA')
    print(dc)
    #get_user_with_openids(['o6hHBv6heaheBEvoPoJMEcsKXyZA'])